<!--
 * @Descripttion: 增加功能点
 * @Author: YuZhenJing
 * @Date: 2020-01-12 12:01:01
 * @LastEditors  : YuZhenJing
 * @LastEditTime : 2020-01-16 09:24:03
 -->
<template>
  <div class="menu-points-add">
    <tf-dialog
      :title="windowInfo.title"
      :title-icon="windowInfo.titleIcon"
      :visible.sync="tfVisible"
    >
      <template v-slot:content>
        <el-card shadow="never">
          <el-form
            :model="formItem"
            :rules="rules"
            label-width="120px"
            ref="menuPointsFrom"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="功能点编号" prop="funPointsCode">
                  <el-input v-model="formItem.funPointsCode"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="功能点名称" prop="funPointsName">
                  <el-input v-model="formItem.funPointsName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="默认选中" prop="defaultSelect">
                  <el-select clearable v-model="formItem.defaultSelect">
                    <el-option
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                      v-for="item in defaultSelectOptions"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </template>
      <template v-slot:footer>
        <el-row :gutter="12" style="padding:5px;">
          <el-col :span="24" style="text-align:center">
            <el-button
              :loading="saveLoading"
              @click="submitForm"
              icon="el-icon-circle-plus-outline"
              type="primary"
              >保存</el-button
            >
          </el-col>
        </el-row>
      </template>
    </tf-dialog>
  </div>
</template>
<script>
export default {
  props: {
    defaultSelectOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    'tf-dialog': require('@/components/common/dialog/TfDialog').default
  },
  data() {
    //功能点编码唯一性验证
    let pointsNameUnique = (rule, value, callback) => {
      const unique = this.$html.get('menu/funPointsCodeIsUnique/' + value)
      unique
        .then(data => {
          if (data.returnType == 'success') {
            if (data.returnData) {
              return callback()
            } else {
              return callback(new Error('【 ' + value + ' 】已经被占用！'))
            }
          }
        })
        .catch(() => {
          console.log('error:pointsNameUnique')
        })
    }
    return {
      tfVisible: false,
      // 窗体信息
      windowInfo: {
        titleIcon: 'el-icon-circle-plus-outline',
        title: '增加功能点'
      },
      saveLoading: false,
      rules: {
        funPointsCode: [
          { required: true, message: '功能点编号不能为空!', trigger: 'blur' },
          {
            pattern: /^[A-Z]*$/,
            message: '功能点编号只能为大写字母!',
            trigger: 'blur'
          },
          {
            max: 36,
            message: '功能点编号不得超过【36】个字符',
            trigger: 'blur'
          },
          { validator: pointsNameUnique, trigger: 'blur' }
        ],
        funPointsName: [
          { required: true, message: '功能点名称不能为空!', trigger: 'blur' },
          {
            max: 18,
            message: '功能点名称不得超过【18】个字符',
            trigger: 'blur'
          }
        ],
        defaultSelect: [
          {
            required: true,
            message: '默认选中状态不能为空!',
            trigger: 'change'
          }
        ]
      },
      formItem: {
        funListCode: '',
        funPointsCode: '',
        funPointsName: '',
        defaultSelect: 'yes'
      }
    }
  },
  methods: {
    // 显示新增窗口
    show(funListCode) {
      if (funListCode) {
        this.formItem.funListCode = funListCode
        if (this.$refs.menuPointsFrom) {
          this.$refs.menuPointsFrom.resetFields()
        }
        this.tfVisible = true
      } else {
        this.$alert('功能清单编码不能为空，无法增加功能点', '提示', {
          type: 'warning',
          confirmButtonText: '确定'
        })
      }
    },
    // 提交表单保存功能点信息
    submitForm() {
      this.$refs.menuPointsFrom.validate(valid => {
        if (valid) {
          this.saveLoading = true
          const saveFormItem = this.$html.post(
            'menu/saveFunPoints',
            this.formItem
          )
          saveFormItem
            .then(data => {
              if (data.returnType == 'success') {
                this.saveLoading = false
                this.$emit('request')
                this.$notify({
                  title: '成功',
                  message: '保存成功',
                  type: 'success'
                })
                this.tfVisible = false
              }
            })
            .catch(() => {
              this.saveLoading = false
              console.log('error:menuPointsAdd/submitForm')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
